<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
include("../function/_nosql.php"); //
include("../function/function.php");
require_once "lib/WxPay.Api.php";
require_once 'lib/WxPay.Notify.php';
require_once 'log.php';

//初始化日志
$logHandler= new CLogFileHandler("logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);
		Log::DEBUG("query:" . json_encode($result));
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}
	
	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{
		Log::DEBUG("call back:" . json_encode($data));
		$notfiyOutput = array();
		
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}
		if($data["result_code"] == 'SUCCESS' ){
		$out_trade_no =$data["out_trade_no"];
			if(strstr($out_trade_no,'_')){
			 $nowdate = date('Y-m-d H:i:s');
			 $enddate = date('Y-m-d H:i:s', strtotime("+1 year"));
			 mysql_query("update kboy_user  set user_style=1 , `user_vipstime` = '".$nowdate."' , `user_vipetime` = '".$enddate."'  where user_trade_no='$out_trade_no'");
			 
			}else{
				$order_no = substr($out_trade_no,0,4);
					if($order_no == "0500"){
						$a = $out_trade_no;
						$b = explode('u',$a);
						$uid = $b['1'];
						$money = 500+75;
						mysql_query("update kboy_user set user_yu_e=user_yu_e+".$money." where uid='$uid'");
						webAD($uid,"充值500","恭喜您充值成功，您的订单号为".$out_trade_no,500);
					}else if($order_no == "0501"){
						$a = $out_trade_no;
						$b = explode('u',$a);
						$uid = $b['1'];
						$money = 1000+150;
						mysql_query("update kboy_user set user_yu_e=user_yu_e+".$money." where uid='$uid'");
						webAD($uid,"充值1000","恭喜您充值成功，您的订单号为".$out_trade_no,1000);
					}else if($order_no == "0502"){
						$a = $out_trade_no;
						$b = explode('u',$a);
						$uid = $b['1'];
						$money = 3000+600;
						mysql_query("update kboy_user set user_yu_e=user_yu_e+".$money." where uid='$uid'");
						webAD($uid,"充值3000","恭喜您充值成功，您的订单号为".$out_trade_no,3000);
					}else{
						mysql_query("update kboy_order set order_state=1 , order_payway=2 where order_no='$out_trade_no'");
						$time = date("Y-m-d H:i:s", time());
						$ztai = getClassName('kboy_jljilu','jl_ztai',"jl_danhao='$rUnique'");
						if($ztai == 0){
						mysql_query("update kboy_jljilu set jl_ztai=1,jl_time='$time' where jl_danhao='$out_trade_no'");
					}
				
				}
				
			}
		}
		
		return true;
	}
}

Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);
